package com.web.controller;

import com.pojo.User;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpSession;
import java.awt.*;


/**
 * @author czy@win10
 * @date 2019/9/19 22:48
 */
@Controller
public class UserController {
	//依赖注入
	@Autowired
	private UserService userService;

    /*
    用户登陆
     */
//    @RequestMapping(value = "/login.action", method = RequestMethod.POST)
//    public String login(String usercode, String password, Model model, HttpSession httpSession, HttpServletRequest request) {
//        //通过账号密码查询用户
//        User user = userService.findUser(usercode, password);
//        if (user != null) {
//            //将用户和用户对象添加到session
//            httpSession.setAttribute("USER_SESSION", user);
//            //跳转到主页
//            return "customer";
//        }
//        model.addAttribute("msg", "账号或密码错误,请从新输入");
//        return "signin";
//    }

	@RequestMapping(value = "/login.action", method = RequestMethod.POST)
	public String login(String usercode, String password, Model model,
	                    HttpSession session) {
		// 通过账号和密码查询用户
		System.out.println(usercode + password);
		User user = userService.findUser(usercode, password);
		System.out.println(user);
		if (user != null) {
			// 将用户对象添加到Session
			session.setAttribute("USER_SESSION", user);
//            // 跳转到主页面
//			return "customer";
			return "redirect:customer/list.action";
		}
		model.addAttribute("msg", "账号或密码错误，请重新输入！");
		// 返回到登录页面
		return "signin";
	}

	//测试登陆验证
	@RequestMapping("/toCustomer.action")
	public String toCustomer() {
		return "customer";
	}

	//退出登陆
	@RequestMapping(value = "/logout.action")
	public String logout(HttpSession session) {
		//清除session
		session.invalidate();
		//跳转到登陆页面
		//使用重定向
		return "redirect:login.action";
	}


	//向用户登陆界面重定向
	@RequestMapping(value = "/login.action", method = RequestMethod.GET)
	public String login() {
		return "signin";
	}


	//跳转注册
	@RequestMapping(value = "/register.action")
	public String register() {

		//跳转到登陆页面
		//使用重定向
		return "redirect:signup.action";
		//return "signup";
	}

	//向用户注册界面重定向
	@RequestMapping(value = "/signup.action", method = RequestMethod.GET)
	public String signup() {
		return "signup";
	}


	@RequestMapping(value = "signup.action", method = RequestMethod.POST)
	public String login(Checkbox check, String user_code, String username1, String username2, String user_password, String user_password1, Model model, HttpSession httpSession) {
		//通过账号密码创建
		if (user_code.equals("") || user_password.equals("") || user_password1.equals("") || username1.equals("") || username2.equals("")) {
			model.addAttribute("msg", "请填写完整信息");
			return "signup";
		} else if (user_password.equals(user_password1) == false) {
			model.addAttribute("msg", "账号或密码不一致,请从新输入!");
			return "signup";
		} else if (check == null) {
			model.addAttribute("msg", "请勾选协议!");
			return "signup";
		} else {
			String user_name = username1 + username2;
			User user = new User(user_code, user_name, user_password);
			System.out.println(user);
			if (user != null) {
				//将用户和用户对象添加到session
				int i = userService.createUser(user);
				if (i != 0) {
					httpSession.setAttribute("USER_SESSION", user);
					//跳转到登陆页面
					return "signin";
				}
			}
		}
		return "signup";
	}
}

